<script>
import InstitutionSelect from '@/components/institutionSelect/institutionSelect.vue'
import { assignedStatusArray, settingListArray } from '@/constants/dispatch/transport'
import FilterPage from '@/components/FilterPage/FilterPage.vue'
import { mapState } from 'vuex'
import { changeAdjustAPI } from '@/api/dispatch/transportAPI'
import TransportForm from '@/views/dispatch/transport/components/transportForm/transportForm.vue'

export default {
  name: 'transport',
  data() {
    return {
      pageParams: {
        page: 1,
        pageSize: 10,
        // 当前所在机构id
        currentAgencyId: undefined,
        // 起始机构ID
        startAgencyId: undefined,
        // 结束机构ID
        endAgencyId: undefined,
        // 调度状态
        assignedStatus: undefined
      },
      startPlaceholder: '请选择起始机构',
      endPlaceholder: '请选择目的机构',
      currentPlaceholder: '请选择所在机构',
      assignedStatusArray,
      settingListArray,
      loading: false,
      dialogVisible: false

    }
  },
  methods: {
    changeAdjustAPI,
    settingList() {
      return settingList
    },
    /*
    *
    * 获取运输任务分页数据的回调函数
    *  */
    async fetchTransportList() {
      try {
        this.loading = true
        await this.$store.dispatch('transportStore/fetchTransportListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /*
    * 获取起始机构ID
    *  */
    getStartAgencyId({ id, name }) {
      this.pageParams.startAgencyId = id
      this.startPlaceholder = name
    },

    /*
    * 获取结束机构ID
    *  */
    getEndAgencyId({ id, name }) {
      this.pageParams.endAgencyId = id
      this.endPlaceholder = name
    },
    /*
    * 获取当前机构ID
    *  */
    getCurrentAgencyId({ id, name }) {
      this.pageParams.currentAgencyId = id
      this.currentPlaceholder = name
    },

    /*
    *  */
    selectStatus(status) {
      this.pageParams.assignedStatus = status
    },

    /*
    * 点击搜索按钮的回调函数
    *  */
    handleSearch() {
      this.fetchTransportList()
    },

    /*
    * pageSize变化的回调
    *  */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchTransportList()

    },

    /*
    * page变化的回调
    *  */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchTransportList()
    },
    /*
    *
    * 取消运输任务的回调函数
    *  */
    async cancelTransport(row) {
      const confirm = await this.$confirm('取消此运输订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const cancelResult = await this.$store.dispatch('transportStore/cancelTransportAction', row.id)
        this.fetchTransportList()
        this.$message.success(cancelResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    /*
    *
    * 手动调整车次的回调函数
    *  */
    changeAdjust(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.transportRef.formData.id=row.id
        this.$refs.transportRef.fetchTrips(row.transportLineId)
        this.$refs.transportRef.fetchVehicleList()
      })
    },
    /*
    *
    * 点击确定按钮的回调函数
    *  */
    async handleSubmit() {
      try {
        const changeResult = await this.$store.dispatch('transportStore/changeAdjustAction', this.$refs.transportRef.formData)
        this.dialogVisible = false
        this.fetchTransportList()
        this.$message.error(changeResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  mounted() {
    this.fetchTransportList()
  },
  computed: {
    ...mapState('transportStore', ['counts', 'transportList'])
  },
  components: { TransportForm, InstitutionSelect, FilterPage }
}
</script>

<template>
  <div class="operation">
    <el-card class="top" shadow="never">
      <el-form inline>
        <el-form-item label="运单编号:">
          <el-input placeholder="请输入运单编号"></el-input>
        </el-form-item>
        <el-form-item label="订单编号:">
          <el-input placeholder="请输入订单编号"></el-input>
        </el-form-item>
        <el-form-item label="调度状态:">
          <el-select v-model="pageParams.assignedStatus">
            <el-option v-for="(item) in assignedStatusArray" :value="item.type" :label="item.label" :key="item.type"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item label="起始机构:">
          <institution-select :placeholder="startPlaceholder" @nodeClick="getStartAgencyId"></institution-select>
        </el-form-item>
        <el-form-item label="目的机构:">
          <institution-select :placeholder="endPlaceholder" @nodeClick="getEndAgencyId"></institution-select>
        </el-form-item>
        <el-form-item label="所在机构:">
          <institution-select :placeholder="currentPlaceholder" @nodeClick="getCurrentAgencyId"></institution-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <filter-page class="filter-page" :status-list="settingListArray" @select="selectStatus"></filter-page>

    <el-card class="bottom" shadow="never" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
      <el-table stripe border :data="transportList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}">
        <el-table-column type="index" label="序号" align="center" width="100px"></el-table-column>
        <el-table-column label="车辆图片" align="center" width="200px">
          <template #default="{row}">
            <el-avatar :size="40" :src="row.truck.picture">
              <img src="https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/slwl/0aefd0d6-2553-4038-a466-7b2618526046.jpg"/>
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="运输任务编号" align="center" width="200px"></el-table-column>
        <el-table-column label="车牌号码" align="center" width="130px">
          <template #default="{row}">{{ row.truck.licensePlate }}</template>
        </el-table-column>
        <el-table-column label="起始地" align="center" width="120px">
          <template #default="{row}">{{ row.startAgency.name }}</template>
        </el-table-column>
        <el-table-column label="目的地" align="center" width="120px">
          <template #default="{row}">{{ row.endAgency.name }}</template>
        </el-table-column>
        <el-table-column prop="status" label="运输任务状态" align="center" width="130px"></el-table-column>
        <el-table-column prop="loadingStatus" label="车辆装载状态" align="center" width="130px"></el-table-column>
        <el-table-column prop="assignedStatus" label="任务分配状态" align="center" width="130px"></el-table-column>
        <el-table-column prop="planDepartureTime" label="计划发车时间" align="center" width="160px"></el-table-column>
        <el-table-column prop="actualDepartureTime" label="实际发车时间" align="center" width="160px"></el-table-column>
        <el-table-column prop="updated" label="更新时间" align="center" width="160px"></el-table-column>
        <el-table-column prop="" label="操作" align="center" fixed="right" width="160px">
          <template #default="{row}">
            <el-button type="text" @click="$router.push({ path:'/transport-detail',query:{id:row.id}})">查看</el-button>
            <el-button type="text" @click="cancelTransport(row)">取消</el-button>
            <el-button type="text" @click="changeAdjust(row)">手动调整</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[2, 3, 4, 10]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="counts" background></el-pagination>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="手动调整">
      <transport-form ref="transportRef"></transport-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<style scoped lang="scss">
.operation {
  padding: 20px;

  .top {
    margin-bottom: 10px;

    .el-form-item {
      width: 380px;

      .el-input {
        width: 139%;
      }

      .el-select {
        width: 130%;
      }
    }
  }

  .center {

  }

  .bottom {
    margin-top: 10px;

    .el-table {
      margin-bottom: 10px;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
